<?php

namespace app\controller\notify;

use app\model\Orders;
use app\queue\CapturePaymentQueue;
use think\facade\Queue;
use think\facade\Log;
use think\helper\Arr;
use think\Request;

class NotifyAirwallex implements NotifyInterface
{

    public function notify(Request $request)
    {
        $json = $request->all();
        $data = Arr::get($json,'data',[]);
        $sourceId = Arr::get($json,'sourceId', '');
        $event = Arr::get($json,'name', '');
        if(empty($data) || empty($sourceId) || empty($event)) throw new \Exception("airwallex params err");
        $order = Orders::query()->where('order_no',$sourceId)->find();
        if(is_null($order)) throw new \Exception("order is err,sourceId: " . $sourceId);
        $error = Arr::get($data,'error', []);
        if($error){
            $exception = sprintf("code: %s, msg: %s",Arr::get($error,'code','00'),Arr::get($error,'message','err'));
            Orders::saveExceptionMessage($order,$exception);
            return $order;
        }
        //支付成功
        if($event == 'payment_intent.succeeded'){
            $status = Arr::get($data,'object.status','');
            if($status == 'SUCCEEDED') Orders::completed($order,$status);
        }
        return $order;

    }

    public function capture(Request $request, Orders $order): bool
    {
         return false;
    }
}